package queue

import "fmt"

/**
链式队列，比较简单
*/
type Node struct {
	value int
	pNext *Node
}

func NewNode(value int) *Node {
	return &Node{
		value: value,
		pNext: nil,
	}
}

type LinkQueue struct {
	head, tail *Node
	size       int
}

func NewLinkQueue() *LinkQueue {
	return &LinkQueue{
		head: nil,
		tail: nil,
		size: 0,
	}
}

func (lq *LinkQueue) Push(value int) {
	node := NewNode(value)

	if lq.size == 0 {
		lq.head = node
		lq.tail = node
	}

	lq.tail.pNext = node
	lq.tail = node

	lq.size++
}

func (lq *LinkQueue) Pop() int {
	if lq.size == 0 {
		return -1
	}

	next := lq.head.pNext
	result := lq.head.value
	lq.head = next
	lq.size--

	return result
}

func (lq *LinkQueue) Print() {
	tmp := lq.head
	for ; tmp != nil; {
		fmt.Printf("%d ", tmp.value)
		tmp = tmp.pNext
	}
	fmt.Println()
}
